package com.udi.project.business.local.gs1.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.udi.common.exception.base.BaseException;
import com.udi.common.utils.security.ShiroUtils;
import com.udi.framework.web.controller.BaseController;
import com.udi.framework.web.domain.AjaxResult;
import com.udi.framework.web.page.TableDataInfo;
import com.udi.project.business.base.domain.BaseCorpVo;
import com.udi.project.business.local.di.service.DiMangeService;
import com.udi.project.business.local.gs1.daomain.AvailableGs1;
import com.udi.project.business.local.gs1.domain.BeUsable;
import com.udi.project.business.local.gs1.domain.Gs1Data;
import com.udi.project.business.local.gs1.domain.SelGsMesVo;
import com.udi.project.business.local.gs1.service.Gs1MangeService;
import com.udi.project.business.pojo.damain.Gs1Udi;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Admin
 * Gs1本地管理器
 */
@Controller
@RequestMapping("/gs1manage")
public class Gs1MangeController extends BaseController {

    private String prefix = "local/gsmana";

    @Autowired
    private Gs1MangeService gs1MangeService;

    @RequiresPermissions("gs:main:view")
    @GetMapping("/main")
    public String gsmain(){
        return prefix+"/gs1main";
    }

    @RequiresPermissions("gs:ls:view")
    @GetMapping("/ls")
    public String gsmainls(){
        return prefix+"/gs1ls";
    }

    @GetMapping("/add")
    public String gsadd(){
        return prefix+"/add";
    }

    @RequestMapping("/edit/{Id}")
    public String corp_edit(@PathVariable("Id") Integer id, ModelMap mmap){
        mmap.put("PI", gs1MangeService.selectGs1ById(id));
        return prefix + "/edit";
    }

    @GetMapping("/detail/{Id}")
    public String detail(@PathVariable("Id") Integer id, ModelMap mmap){
        mmap.put("PI", gs1MangeService.selectGs1ById(id));
        System.out.println(gs1MangeService.selectGs1ById(id));
        return prefix + "/detail";
    }

    @GetMapping("/detaills/{Id}")
    public String detaills(@PathVariable("Id") Integer id, ModelMap mmap){
        mmap.put("PI", gs1MangeService.selectGs1ById(id));
        return prefix + "/detaills";
    }

    /**
     * @Description: GS1-128列表
     * @param selGsMesVo
     * @return
     */
    @RequestMapping("/getlist")
    @ResponseBody
    public TableDataInfo getlist(SelGsMesVo selGsMesVo){
        startPage();
        List<SelGsMesVo> list = gs1MangeService.selectUdi(selGsMesVo);
        return getDataTable(list);
    }

    /**
     * @Description: 查询GS1-128列表
     * @param selGsMesVo
     * @return
     */
    @RequestMapping("/getlistls")
    @ResponseBody
    public TableDataInfo getlistls(SelGsMesVo selGsMesVo){
        startPage();
        List<SelGsMesVo> list = gs1MangeService.selectUdils(selGsMesVo);
        return getDataTable(list);
    }


    /**
     * GS1-123编辑
     * @param selGsMesVo
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult getadd(SelGsMesVo selGsMesVo){
        System.out.println("--------------->"+selGsMesVo);
        try{
            return toAjax(gs1MangeService.updateGs1(selGsMesVo));
        }catch(BaseException e){
            return error(e.getMessage());
        }
    }

    /**
     * 获取应用标识符
     * @return
     */
    @RequestMapping("/getAi")
    @ResponseBody
    public JSONObject getAi(){
        JSONObject obj = new JSONObject();
        try {
            List<Gs1Data> gs1Data = gs1MangeService.getAi();
            JSONArray array= JSONArray.parseArray(JSON.toJSONString(gs1Data));
            obj.put("code",1);
            obj.put("msg",array);
            return obj;
        }catch(BaseException e){
            obj.put("code",0);
            obj.put("msg",e.getMessage());
            return obj;
        }
    }

    /**
     * 删除gs1-128信息
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        try {
            return toAjax(gs1MangeService.remove(ids));
        }catch (BaseException e){
            return error(e.getMessage());
        }
    }
    /**
     * 永久删除gs1-128信息
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(String ids){
        try {
            return toAjax(gs1MangeService.delete(ids));
        }catch (BaseException e){
            return error(e.getMessage());
        }
    }



    @RequestMapping("/seUdidi")
    @ResponseBody
    public List<Gs1Udi> seUdiDi(String id){
        return gs1MangeService.selectUdiDi(id);
    }


    @RequestMapping("/deUdi")
    @ResponseBody
    public String deUdi(List<String> id){
        return gs1MangeService.deleteUdi(id);
    }



    /**
    *
     * @Description: 查找可使用的GS1编码，此编码用于打印列表，与excel交互
     * @param
     * @return: java.util.List<com.udi.project.business.local.gs1.daomain.AvailableGs1>
     * @Date: 2021/8/18 19:14
     */
    @RequestMapping("/seGS1")
    @ResponseBody
    public List<AvailableGs1> seGS1(){
        return gs1MangeService.selectGS1();
    }


    @Autowired
    DiMangeService diMangeService;

    /**
     * 返回部门名与部门号
     * @return
     */
    @RequestMapping("/getDeptMes")
    @ResponseBody
    public JSONArray sel(){
        JSONObject current = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String currdeptId = (String)((JSONObject)current.get("dept")).getString("deptId");
        String roleKey =((JSONObject)((JSONArray)current.get("roles")).get(0)).getString("roleKey");
        if(roleKey.contains("admin")){
            List<BaseCorpVo> list = diMangeService.getdept("");
            return JSONArray.parseArray(JSON.toJSONString(list));
        }else{
            List<BaseCorpVo> list = diMangeService.getdept(currdeptId);
            return JSONArray.parseArray(JSON.toJSONString(list));
        }
    }

    /**
     * @Description: 模糊查询di
     * @param data 产品
     * @return: 产品名称，规格型号，di码，di码对应的di_id
     * @Date: 2021/8/20 9:41
     * http://localhost:8080/Gs1Generate/getdi?cpmctymc=产品&ggxh=规格
     */
    @RequestMapping("/getDi")
    @ResponseBody
    public JSONArray getDi(String data){
        List<BeUsable> list = gs1MangeService.getDi(data);
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
        return array;
    }
}
